package com.ghost.manager
{
	import com.ghost.model.Global_data;
	import com.ghost.model.Media_base;
	import com.ghost.model.Object_pool;
	
	import flash.net.NetConnection;

	/**
	 * 播放高品质音乐 
	 * @author ghost
	 */	
	public class HDmedia_manager
	{
		private static var instance:HDmedia_manager;
		public static function getInstance():HDmedia_manager
		{
			if(instance == null)
			{
				instance = new HDmedia_manager();
			}
			return instance;
		}
		public var connect_nc:NetConnection;
		public var playing_music_v:Vector.<String>;//存放正在播放的音乐
		public function HDmedia_manager()
		{
			connect_nc = new NetConnection();
			connect_nc.connect(null);
			playing_music_v = new Vector.<String>();
		}
		/**
		 * 播放高品质声音
		 * @param url 高品质声音路径
		 * @param music_type 0-音乐 1-音效
		 */		
		public function play_m4a(url:String,music_type:int=0):void
		{
			if(!Object_pool.sound_obj_pool.containsKey(url))
			{
				var media:Media_base = new Media_base();
				media.type = "music";
				media.url = url;
				media.music_type = music_type;
				Object_pool.sound_obj_pool.put(url,media);
				media.type_init();
				media.play();
			}
			else
			{
				Media_base(Object_pool.sound_obj_pool.get(url)).type_init();
				Media_base(Object_pool.sound_obj_pool.get(url)).play();
			}
			playing_music_v.push(url);
			if(!Global_data.music_bool)Media_base(Object_pool.sound_obj_pool.get(url)).stop();
		}
		/**
		 * 停止播放高品质声音
		 * @param url 高品质声音路径
		 */		
		public function stop_m4a(url:String):void
		{
			var n:int = playing_music_v.indexOf(url);
			if(n!=-1)
			{
				playing_music_v.splice(n,1);
				Media_base(Object_pool.sound_obj_pool.get(url)).stop();
			}
		}
		/**
		 *开启播放所有流上应该播放的音乐 功能
		 */		
		public function play_all_music():void
		{
			var n:int = playing_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				Media_base(Object_pool.sound_obj_pool.get(playing_music_v[i])).play();
			}
			Global_data.music_bool = true;
		}
		/**
		 *关闭播放所有流上应该停止的音乐 功能
		 */		
		public function stop_all_music():void
		{
			var n:int = playing_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				Media_base(Object_pool.sound_obj_pool.get(playing_music_v[i])).stop();
			}
			Global_data.music_bool = false;
		}
		/**
		 * 改变所有音量
		 */		
		public function change_vol():void
		{
			var n:int = playing_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				Media_base(Object_pool.sound_obj_pool.get(playing_music_v[i])).type_init();
			}
		}
	}
}